/**
 * Project: lionbule-other
 * 
 * File Created at 2012-4-7
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;

/**
 * TestThreadLoader.
 * @author LionBule
 *  2012-4-7 下午2:00:12
 */
public class TestThreadLoader {

    public static void main(String[] args) throws Exception {
        TestThreadLoader ttl = new TestThreadLoader();
        
        URL url = ttl.getClass().getResource("/").toURI().toURL();
        
        String path = "D:\\test.jar";
        
        URL testUrl = (new File(path)).toURL();
        
        TestClassLoader mc_01 = new TestClassLoader(new URL[]{testUrl});
        TestClassLoader mc_02 = new TestClassLoader(new URL[]{testUrl});
        
        test_01(mc_01);
        //test_02(mc_02);
        
        System.out.println("111");
        
    }
    
    static void test_01(TestClassLoader mcl) throws Exception{
//        ClassLoader clOld = Thread.currentThread().getContextClassLoader();
//        Thread.currentThread().setContextClassLoader(mcl);
        
        Class<?> cla = Class.forName("lb.test.TestLionbule", true,
                mcl);
        Method ts_m = cla.getMethod("inc", null);
        ts_m.invoke(cla.newInstance(), null); 
        ts_m.invoke(cla.newInstance(), null);
        ts_m.invoke(cla.newInstance(), null);
        
        
        Class<?> cla_1 = Class.forName("lb.test.TestLionbule", true,
                mcl);
        Method ts_m_1 = cla_1.getMethod("inc", null);
        ts_m_1.invoke(cla_1.newInstance(), null); 
        ts_m_1.invoke(cla_1.newInstance(), null);
        ts_m_1.invoke(cla_1.newInstance(), null);
        
//        Thread.currentThread().setContextClassLoader(clOld);
    }
    
    static void test_02(TestClassLoader mcl) throws Exception{
        ClassLoader clOld = Thread.currentThread().getContextClassLoader();
        
        Thread.currentThread().setContextClassLoader(mcl);
        
        Class<?> cla = Class.forName("lb.test.TestLionbule", true,
                mcl);
        Method ts_m = cla.getMethod("inc", null);
        ts_m.invoke(cla.newInstance(), null); 
        ts_m.invoke(cla.newInstance(), null);
        ts_m.invoke(cla.newInstance(), null);
        
        Thread.currentThread().setContextClassLoader(clOld);
    }    

}
